package com.sx.gongzhong.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.ConvenniceAppealAdapter;
import com.sx.gongzhong.model.ConvenniceAppealBean;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.pest.ConvenniceAppealListPest;
import com.sx.gongzhong.utils.AppConstants;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.ConvenniceAppealView;
import com.sx.gongzhong.widget.MyPopupWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**便民诉求
 * Created by admin on 2018/1/3.
 */

public class ConvenniceAppealActivity  extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,  ConvenniceAppealView, View.OnClickListener {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private int page = 1, pagesize = 10;
    private ConvenniceAppealAdapter adapter;
    private List<ConvenniceAppealBean> mlist;

    private MyPopupWindow selectPop;
    private Disposable disposable;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.refresh_list);
        ButterKnife.bind(this);
        setTitleName("便民诉求");
        findViewById(R.id.imv_Right).setOnClickListener(this);
        setRightImage(R.mipmap.icon_nav_add);
        initRx();
        initView();
    }

    private void initView() {
        mlist = new ArrayList<>();
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(ConvenniceAppealActivity.this, LinearLayoutManager.VERTICAL, false));
        adapter = new ConvenniceAppealAdapter(this, R.layout.item_convenniceappeal, mlist);
        adapter.setEmptyView(View.inflate(this, R.layout.load_error_view, null));
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        }, recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(ConvenniceAppealActivity.this,ConvenniceAppealDetailActivity.class)
                        .putExtra("appealId",mlist.get(position).id));
            }
        });
        onRefresh();
    }

    private void initRx() {
        disposable =  MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent msgEvent) throws Exception {
                        if ("ConvenniceAppealActivity".equals(msgEvent.getRequest())) {
                            if (msgEvent.getType()== 1){
                                page = 1;
                                getData();
                            }
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() ==  R.id.tv_Cancel){
            selectPop.dismiss();
            return;
        }
        if (v.getId() == R.id.imv_Right){
            if (isLogin()){
                showPopupSelect();
            }
            return;
        }
        int type=0;
        switch (v.getId()){
            case R.id.tv_String:
                type=0;
                break;
            case R.id.tv_Picture:
                type=1;
                break;
            case R.id.tv_Audio:
                type=2;
                break;
            case R.id.tv_Video:
                type=3;
                break;
        }
        selectPop.dismiss();
        startActivity(new Intent(ConvenniceAppealActivity.this,AddConvenniceAppealActivity.class)
                .putExtra("TYPE",type));
    }
    /**
     * 显示选择文件的弹窗
     */
    private void showPopupSelect() {
        if (selectPop == null) {
            View view = LayoutInflater.from(this).inflate(R.layout.pop_select_file, null);
            selectPop = new MyPopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            selectPop.setTouchable(true);
            selectPop.setOutsideTouchable(false);
            selectPop.setBackgroundDrawable(new ColorDrawable(Color.alpha(128)));
            view.findViewById(R.id.tv_String).setOnClickListener(this);
            view.findViewById(R.id.tv_Picture).setOnClickListener(this);
            view.findViewById(R.id.tv_Audio).setOnClickListener(this);
            view.findViewById(R.id.tv_Video).setOnClickListener(this);
            view.findViewById(R.id.tv_Cancel).setOnClickListener(this);
        }
        selectPop.showAsDropDown(findViewById(R.id.toolbar));
    }
    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(false);
        page = 1;
        getData();
    }

    private void getData() {
        new ConvenniceAppealListPest(this).getConvenniceAppealData(this,page,pagesize);
    }

    @Override
    public void getSuccess(List<ConvenniceAppealBean> list,int total) {
        if (page == 1) {
            mlist.clear();
            adapter.setNewData(mlist);
        }
        if (list != null && list.size() > 0) {
            mlist.addAll(list);
        }
        if (page == total) {
            adapter.loadMoreEnd();
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(ConvenniceAppealActivity.this,message);
    }

    @Override
    protected void onDestroy() {
        if (disposable!= null) {
            disposable = null;
        }
        super.onDestroy();
    }
}
